//
//  ILMusicDetailView.m
//  ILife
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 ILife. All rights reserved.
//

#import "ILMusicDetailView.h"
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>
@interface ILMusicDetailView ()<UIWebViewDelegate>
@property(nonatomic, strong)UIView *headerView;
@property(nonatomic, strong)UILabel *singerLabel;


@property(nonatomic, strong)UILabel *songLabel;
@property(nonatomic, strong)UILabel *tingNumLabel;
@property(nonatomic, strong)UILabel *seeNumLabel;
@property(nonatomic, strong)UIImageView *seeNumImageView;
@property(nonatomic, strong)UIImageView *tingNumImageView;

@end

@implementation ILMusicDetailView


-(void)playControlInit
{
    _songLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 20, SCREEN_WIDTH - 140, 30)];
    _songLabel.numberOfLines = 0;
    _songLabel.font = [UIFont systemFontOfSize:18];
//  _songLabel.textAlignment = NSTextAlignmentCenter;
    
   
    _seeNumImageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 70, 20, 20)];
    [_playView addSubview:_seeNumImageView];
    _seeNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 70, 100, 20)];
    _seeNumLabel.font = [UIFont systemFontOfSize:10];
    
    _tingNumImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 70, 20, 20)];
    [_playView addSubview:_tingNumImageView];
    _tingNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 70, 100, 20)];
    _tingNumLabel.font =[UIFont systemFontOfSize:10];
    
    _playButton  =[UIButton buttonWithType:(UIButtonTypeCustom)];
    _playButton.frame =CGRectMake(SCREEN_WIDTH - 100, 10, 70, 70);

    
    
    _playImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 10, 70, 70)];
    _playImageView.userInteractionEnabled = YES;
    [_playImageView.layer setMasksToBounds:YES];
    [_playImageView.layer setCornerRadius:35];

    
    
    
    [_playView addSubview:_songLabel];
    [_playView addSubview:_seeNumLabel];
    [_playView addSubview:_tingNumLabel];
    [_playView addSubview:_playImageView];
    [_playView addSubview:_playButton];
}



-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/2+200)];
        
        _musicImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/2)];
        [_headerView addSubview:_musicImageView];
        
        _playView = [[RS_SliderView alloc]initWithFrame:CGRectMake(0,SCREEN_WIDTH/2+20, SCREEN_WIDTH, 100) andOrientation:Horizontal];
        _playView.backgroundColor = [UIColor grayColor];
        [_playView setColorsForBackground:[UIColor colorWithWhite:0.8 alpha:0.3] foreground:[UIColor colorWithWhite:0.8 alpha:0.5] handle:[UIColor colorWithRed:0.0 green:205.0/255.0 blue:184.0/255.0 alpha:1.0] border:[UIColor colorWithWhite:0.5 alpha:0.5]];
        [_playView hideHandle];
        [_headerView addSubview:_playView];
        [self playControlInit];
        
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(20, SCREEN_WIDTH/2+140, SCREEN_WIDTH-40,1)];
       
        [_headerView addSubview:_webView];
        
        _singerLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, SCREEN_WIDTH/2+160, 100, 30)];
        _singerLabel.font = [UIFont systemFontOfSize:12];
        [_headerView addSubview:_singerLabel];
        

        _recommendTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _recommendTableView.tableHeaderView = _headerView;
        [self addSubview:_recommendTableView];
    }
    return self;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat webHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"]floatValue];
    NSLog(@"webheight = %f",webHeight);
    _webView.frame = CGRectMake(20, SCREEN_WIDTH/2+140, SCREEN_WIDTH-40, webHeight);
 
    CGRect singerFrame = _singerLabel.frame;
    singerFrame.origin.y = webHeight + SCREEN_WIDTH/2+140 +20;
    _singerLabel.frame = singerFrame;
    
    CGRect newFrame = _headerView.frame;
    newFrame.size.height = webHeight + SCREEN_WIDTH/2+140 +50;
    _headerView.frame = newFrame;
    _recommendTableView.tableHeaderView = _headerView;
    
}

-(void)setMusicPlayModel:(ILMusicPlayModel *)musicPlayModel
{
    _musicPlayModel = musicPlayModel;
    
     NSString *string = [NSString stringWithFormat:@"代表作：%@",musicPlayModel.articleData.songname];
    
    _seeNumImageView.image = [UIImage imageNamed:@"iconfont-dajiazaiting"];
    _songLabel.text = string;
    CGFloat height = [ILMusicDetailView heightOfString:string];
    _songLabel.frame = CGRectMake(40, 20, SCREEN_WIDTH - 140, height);
    
    _tingNumImageView.image = [UIImage imageNamed:@"iconfont-ting-2"];
     _tingNumLabel.text = [NSString stringWithFormat:@"%ld",(long)musicPlayModel.hits];
    _seeNumLabel.text = [NSString stringWithFormat:@"%ld",(long)musicPlayModel.articleData.bfnum];
  //  [_playButton sd_setBackgroundImageWithURL:[NSURL URLWithString:musicPlayModel.articleData.songphoto] forState:(UIControlStateNormal)];
    [_playButton setBackgroundImage:[UIImage imageNamed:@"play"] forState:(UIControlStateNormal)];
    [_playImageView sd_setImageWithURL:[NSURL URLWithString:musicPlayModel.articleData.songphoto]];
    
    _singerLabel.text = @"推荐乐人";
    [_musicImageView sd_setImageWithURL:[NSURL URLWithString:musicPlayModel.image]placeholderImage:[UIImage imageNamed:@"ilife"]];
    
    NSString *HTMLString = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:musicPlayModel.articleUrl]] encoding:NSUTF8StringEncoding];
    NSString *deleteStr = [NSString stringWithFormat:@"<header> %@</header>",musicPlayModel.title];
    HTMLString = [HTMLString stringByReplacingOccurrencesOfString:deleteStr withString:@""];
    //NSLog(@"htmlstring =%@",HTMLString);
    [_webView loadHTMLString:HTMLString baseURL:nil];
    _webView.delegate =self;
    
}

+(CGFloat)heightOfString:(NSString *)musicDescription
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18] forKey:NSFontAttributeName];
    
    CGRect bounds = [musicDescription boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 140, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return bounds.size.height;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
